<?php

namespace app\common\library\seo\instance;

use app\common\library\seo\SeoAbstract;
use app\common\library\seo\SeoInterface;
use fast\Http;

class BaiduSeo extends SeoAbstract implements SeoInterface
{

    const TOKEN = 'wo2bXFZK1MFRu0j2';

    const SITE = "www.xcd.icu";

    const BASE_URL = "http://data.zz.baidu.com/urls";

    public function post(array $urls): bool
    {
        try {
            $response = Http::post(
                sprintf("%s?site=%s&token=%s", self::BASE_URL, self::SITE, self::TOKEN),
                join(',', $urls),
                array(
                    CURLOPT_HTTPHEADER => [
                        'Content-Type: text/plain'
                    ],
                ),
            );
            $data = json_decode($response, true);
            if (isset($data['error'])) {
                throw new \Exception($data['message']);
            }
        } catch (\Throwable $exception) {
            return $this->setMessage($exception->getMessage());
        }
        return true;
    }


}